﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Linq;
using System.IO;

namespace QuickTasks.ViewModels.Data
{
    public class RetriveData
    {
       
        
        public string remails(string rec)
        {
            string remail = "";
            using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
            {

               List<string> st= retrive(rec);
               foreach (var s in st)
               {
                   string name = s.Substring(0, s.IndexOf('('));
                   string type = s.Substring(s.IndexOf('(') + 1, s.IndexOf(')') - s.IndexOf('(') - 1);
                   var FavEmails = from i in context.people where i.name == name select i;
                   foreach (var objlist in FavEmails)
                   {
                       if (remail != "")
                       {
                           if (type == "work")
                               remail = remail + ";" + objlist.work;
                            if (type == "other")
                                remail = remail + ";" + objlist.other;
                            if (type == "Personal")
                                remail = remail + ";" + objlist.personal;
                       }
                       else
                           if (type == "work")
                           remail = objlist.work;
                       if (type == "other")
                           remail =objlist.other;
                       if (type == "Personal")
                           remail = objlist.personal;
                   }
               }
            }
            return remail;
        }
        public string cemails(string cc)
        {
            string cemail = "";
            using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
            {
                List<string> st = retrive(cc);
                foreach (var s in st)
                {
                    string name = s.Substring(0, s.IndexOf('('));
                    string type = s.Substring(s.IndexOf('(') + 1, s.IndexOf(')') - s.IndexOf('(') - 1);
                    var FavEmails = from i in context.people where i.name == name select i;
                    foreach (var objlist in FavEmails)
                    {
                        if (cemail != "")
                        {
                            if (type == "work")
                                cemail = cemail + ";" + objlist.work;
                            if (type == "other")
                                cemail = cemail + ";" + objlist.other;
                            if (type == "personal")
                                cemail = cemail + ";" + objlist.personal;
                        }
                        else
                        {
                            if (type == "work")
                                cemail =  objlist.work;
                            if (type == "other")
                                cemail = objlist.other;
                            if (type == "personal")
                                cemail =  objlist.personal;
                           
                        }
                    }
                }
            }
           
            return cemail;
        }
        public string mnos(string user)
        {
            string no = "";
            using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
            {
                List<string> st = retrive(user);
                foreach (var s in st)
                {
                    string name = s.Substring(0, s.IndexOf('('));
                    string type = s.Substring(s.IndexOf('(') + 1, s.IndexOf(')') - s.IndexOf('(') - 1);
                    var FavEmails = from i in context.people where i.name == name select i;
                    foreach (var objlist in FavEmails)
                    {
                        if (no != "")
                        {
                            if (type == "mobile")
                                no = no + ";" + objlist.mobile;
                            if (type == "home")
                                no = no + ";" + objlist.home;
                            if (type == "mwork")
                                no = no + ";" + objlist.mwork;
                           
                        }
                        else
                        {
                            if (type == "mobile")
                                no = objlist.mobile;
                            if (type == "home")
                                no =  objlist.home;
                            if (type == "mwork")
                                no = objlist.mwork;
                        }
                    }
                }
            }
           
            return no;
        }
        public List<string> retrive(string s)
        {
            List<string> str = new List<string>();
           
            string[] Emails = s.Split(new char[] { ',', ';' });
            foreach (string str1 in Emails)
            {
                str.Add(str1);
            }
            return str;

        }
    }
}
